package com.inifitness.admin.core.system.service.impl.oss;


import java.io.InputStream;
import java.time.LocalDateTime;

import jakarta.annotation.PostConstruct;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.ObjectMetadata;
import com.aliyun.oss.model.PutObjectRequest;
import com.inifitness.admin.core.system.model.dto.FileInfo;
import com.inifitness.admin.core.system.service.OssService;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.IdUtil;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

/**
 * Aliyun 对象存储服务类
 *
 * @author sunjinfeng
 * @since 2024/04/18 11:00:00
 */
@Component
@ConditionalOnProperty(value = "oss.type", havingValue = "aliyun")
@ConfigurationProperties(prefix = "oss.aliyun")
@RequiredArgsConstructor
@Data
public class AliyunOssService implements OssService {

  /**
   * 服务Endpoint
   */
  private String endpoint;
  /**
   * 访问凭据
   */
  private String accessKeyId;
  /**
   * 凭据密钥
   */
  private String accessKeySecret;
  /**
   * 存储桶名称
   */
  private String bucketName;

  private OSS aliyunOssClient;

  @PostConstruct
  public void init() {
    aliyunOssClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
  }

  @Override
  @SneakyThrows
  public FileInfo uploadFile(MultipartFile file) {

    // 生成文件名(日期文件夹)
    String suffix = FileUtil.getSuffix(file.getOriginalFilename());
    String uuid = IdUtil.simpleUUID();
    String fileName =
        DateUtil.format(LocalDateTime.now(), "yyyyMMdd") + FileUtil.PATH_SEPARATOR + uuid + "."
            + suffix;
    //  try-with-resource 语法糖自动释放流
    try (InputStream inputStream = file.getInputStream()) {

      // 设置上传文件的元信息，例如Content-Type
      ObjectMetadata metadata = new ObjectMetadata();
      metadata.setContentType(file.getContentType());
      // 创建PutObjectRequest对象，指定Bucket名称、对象名称和输入流
      PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, fileName, inputStream,
          metadata);
      // 上传文件
      aliyunOssClient.putObject(putObjectRequest);
    }
    // 获取文件访问路径
    String fileUrl = "https://" + bucketName + "." + endpoint + "/" + fileName;
    FileInfo fileInfo = new FileInfo();
    fileInfo.setName(fileName);
    fileInfo.setUrl(fileUrl);
    return fileInfo;
  }

  @Override
  public boolean deleteFile(String filePath) {
    Assert.notBlank(filePath, "删除文件路径不能为空");
    // 文件主机域名
    String fileHost = "https://" + bucketName + "." + endpoint;
    // +1 是/占一个字符，截断左闭右开
    String fileName = filePath.substring(fileHost.length() + 1);
    aliyunOssClient.deleteObject(bucketName, fileName);
    return true;
  }
}
